#include <iostream>
#include<unistd.h>
#include<pthread.h>
#include<string.h>

using namespace std;
pthread_mutex_t mutex;
pthread_cond_t cond;//条件变量
void* ticket_fun(void* args)
{
  pthread_detach(pthread_self());
  while(true)
  {
    // 执行下面代码，某个场景不就绪，线程阻塞等待
    pthread_cond_wait(&cond,&mutex);//让给对应的线程等待被唤醒
    cout<<"thread id: "<<pthread_self()<<" run..."<<endl;
  }
}

int main()
{
  pthread_mutex_init(&mutex,nullptr);//初始化锁
  pthread_cond_init(&cond,nullptr);
  pthread_t tid1,tid2,tid3;
  pthread_create(&tid1,nullptr,ticket_fun,(void*)"thread1");
  pthread_create(&tid2,nullptr,ticket_fun,(void*)"thread2");
  pthread_create(&tid3,nullptr,ticket_fun,(void*)"thread3");
  
  while(1)
  {
    char n='a';
    cout<<"请输入你的command(n/q):";
    cin>>n;
    if(n=='n') pthread_cond_signal(&cond);
    else 
    {
      break;
    }
  }
  
 // pthread_join(tid1,nullptr);
 // pthread_join(tid2,nullptr);
 // pthread_join(tid3,nullptr);
  //释放锁
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&cond);//释放条件变量
  return 0;
}
